/**
 *
 */
package org.insia.smartposse.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.insia.smartposse.form.ProjectForm;
import org.insia.smartposse.manager.ProjectManager;
import org.insia.smartposse.manager.UserManager;
import org.insia.smartposse.manager.list.CategorieMapManager;

/**
 * @author cmougel
 *
 */
public class ProjectAction
extends DispatchAction
{

	public ActionForward view(ActionMapping map, ActionForm form, HttpServletRequest req, HttpServletResponse resp)
	throws Exception {
		req.setAttribute("view", true);

		ProjectManager proMan = new ProjectManager(new Integer(req.getParameter("id")));

		if (null == proMan.getProject()){
			resp.setStatus(404);
			return null;
		}

		req.setAttribute("project", proMan);

		return map.findForward("view");
	}


	public ActionForward create(ActionMapping map, ActionForm form, HttpServletRequest req, HttpServletResponse arg3)
	throws Exception {

		ProjectForm proForm = (ProjectForm) form;
		proForm.setManagerId(((UserManager) req.getSession().getAttribute("usermanager")).getId());
		req.setAttribute("categoryMap", new CategorieMapManager().getMap());
		req.setAttribute("create", true);

		return map.findForward("create");

	}

	public ActionForward add(ActionMapping map, ActionForm form, HttpServletRequest req, HttpServletResponse resp)
	throws Exception {
		req.setAttribute("view", true);
		ProjectForm projectForm = (ProjectForm)form;
		ProjectManager projectManage = new ProjectManager(projectForm);
		projectManage.save();
		resp.sendRedirect(req.getContextPath() + "/project.do?cmd=view&id=" + projectManage.getId());
		return null;
	}



}
